#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QRect>
#include <QShortcut>
#include <QLabel>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->horizontalSlider->setMinimum(400);
    ui->horizontalSlider->setMaximum(800);
    // 步长
    int step_h = 20;
    int step_v = 20;
    ui->horizontalSlider->setSingleStep(step_h);
    ui->horizontalSlider->setSliderPosition(480);


    ui->verticalSlider->setMinimum(400);
    ui->verticalSlider->setMaximum(1600);
    ui->verticalSlider->setSingleStep(step_v);
    ui->verticalSlider->setSliderPosition(500);

    QShortcut* sub_h = new QShortcut(this);
    sub_h->setKey(Qt::Key_S);
    QShortcut* sub_v = new QShortcut(this);
    sub_v->setKey(Qt::Key_A);
    QShortcut* add_h = new QShortcut(this);
    add_h->setKey(QKeySequence("w"));
    QShortcut* add_v = new QShortcut(this);
    add_v->setKey(QKeySequence("d"));

    connect(sub_h,&QShortcut::activated,this,[=](){
        int val = ui->horizontalSlider->value();
        ui->horizontalSlider->setValue(val - step_h);

    });

    connect(add_h,&QShortcut::activated,this,[=](){
        int val = ui->horizontalSlider->value();
        ui->horizontalSlider->setValue(val + step_h);
    });

    connect(sub_v,&QShortcut::activated,this,[=](){
        int val = ui->verticalSlider->value();
        ui->verticalSlider->setValue(val - step_h);
    });


    connect(add_v,&QShortcut::activated,this,[=](){
        int val = ui->verticalSlider->value();
        ui->verticalSlider->setValue(val + step_h);
    });



}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_horizontalSlider_valueChanged(int value)
{
    qDebug() << "horizontalSlider " << value;
    ui->label->setText("height = "+ QString::number(value));
    QRect rect = this->geometry();
    this->setGeometry(rect.x(),rect.y(),rect.width(),value);
}

void Widget::on_verticalSlider_valueChanged(int value)
{
     qDebug() << "verticalSlider " <<value;
     ui->label_2->setText("width = " + QString::number(value));
     QRect rect = this->geometry();
     this->setGeometry(rect.x(),rect.y(),value,rect.height());
}
